import java.util.*;

public class DataManager {
    private static DataManager instance;
    private List<Class> classes;
    private List<Group> groups;
    private List<Student> students;
    private Random random;
    
    private DataManager() {
        classes = new ArrayList<>();
        groups = new ArrayList<>();
        students = new ArrayList<>();
        random = new Random();
    }
    
    public static DataManager getInstance() {
        if (instance == null) {
            instance = new DataManager();
        }
        return instance;
    }
    
    // 班级管理方法
    public void addClass(String className) {
        int id = classes.size() + 1;
        classes.add(new Class(id, className));
    }
    
    public void removeClass(int classId) {
        classes.removeIf(c -> c.getId() == classId);
    }
    
    public List<Class> getClasses() {
        return new ArrayList<>(classes);
    }
    
    // 小组管理方法
    public void addGroup(String groupName, int classId) {
        int id = groups.size() + 1;
        groups.add(new Group(id, groupName, classId));
    }
    
    public void removeGroup(int groupId) {
        groups.removeIf(g -> g.getId() == groupId);
    }
    
    public List<Group> getGroups() {
        return new ArrayList<>(groups);
    }
    
    // 学生管理方法
    public void addStudent(String name, int classId, int groupId) {
        int id = students.size() + 1;
        students.add(new Student(id, name, classId, groupId));
    }
    
    public void removeStudent(int studentId) {
        students.removeIf(s -> s.getId() == studentId);
    }
    
    public List<Student> getStudents() {
        return new ArrayList<>(students);
    }
    
    // 随机选择方法
    public Group getRandomGroup() {
        if (groups.isEmpty()) return null;
        return groups.get(random.nextInt(groups.size()));
    }
    
    public Student getRandomStudentFromGroup(int groupId) {
        List<Student> groupStudents = students.stream()
            .filter(s -> s.getGroupId() == groupId)
            .toList();
        if (groupStudents.isEmpty()) return null;
        return groupStudents.get(random.nextInt(groupStudents.size()));
    }
    
    public Student getRandomStudentFromClass(int classId) {
        List<Student> classStudents = students.stream()
            .filter(s -> s.getClassId() == classId)
            .toList();
        if (classStudents.isEmpty()) return null;
        return classStudents.get(random.nextInt(classStudents.size()));
    }
    
    // 记录学生表现
    public void recordAttendance(int studentId, int status) {
        students.stream()
            .filter(s -> s.getId() == studentId)
            .findFirst()
            .ifPresent(s -> s.recordAttendance(status));
    }
    
    public void recordScore(int studentId, int score) {
        students.stream()
            .filter(s -> s.getId() == studentId)
            .findFirst()
            .ifPresent(s -> s.addScore(score));
    }
} 